#include "HufArc.h"
fileHead ** listingFile (FILE * arc)
{

	fileHead ** fileHd = NULL;
	unsigned int count = 1;
	char buf[] = "";
	char strInt[8];
	int rc = 0;
	int ch = 0;
	
	fseek (arc,7,SEEK_SET);
	while ( (rc = fread (buf,sizeof(char),NAME_LEN,arc)) == NAME_LEN)
	{
		fileHd = (fileHead **) realloc (fileHd,sizeof(fileHead *)*count);
		if (!fileHd)
			cl_exit (LISTING_FILE,MEMORY_ERR,arc,fileHd);
		fileHd[count - 1] = (fileHead *) calloc (1,sizeof(fileHead));
		if (!fileHd[count - 1])
			cl_exit (LISTING_FILE,MEMORY_ERR,arc,fileHd);
		fileHd[count - 1]->sizeName = strToInt (buf,1);
		fileHd[count - 1]->fileName = (char *) calloc (fileHd[count - 1]->sizeName + 1, sizeof(char));
		if (!fileHd[count - 1]->fileName)
			cl_exit (LISTING_FILE,MEMORY_ERR,arc,fileHd);
		if ((rc = fread (fileHd[count - 1]->fileName,sizeof(char),fileHd[count - 1]->sizeName,arc)) != fileHd[count - 1]->sizeName)
		{
			cl_exit (LISTING_FILE,READ_ERR,arc,fileHd);
			return NULL;
		}
		fileHd[count - 1]->fileName[fileHd[count - 1]->sizeName] = 0;
		rc = fread (strInt,sizeof(char),SIZE_T_8,arc);
		if (rc != SIZE_T_8)
		{
			cl_exit (LISTING_FILE,READ_ERR,arc,fileHd);
			return NULL;
		}
		fileHd[count - 1]->mtime = strToInt (strInt, SIZE_T_8);
		rc = fread (buf,sizeof(char),SIZE_T_1,arc);
		if (rc != 1)
		{
			cl_exit (LISTING_FILE,READ_ERR,arc,fileHd);
			return NULL;
		}
		fileHd[count - 1]->huff_ubits = strToInt (buf,SIZE_T_1);
		if ((fileHd[count - 1]->huff_ubits & LEN_8_B))
			{
				rc = fread (strInt,sizeof(char),SIZE_T_8,arc);
			if (rc != SIZE_T_8) 
			{
				cl_exit (LISTING_FILE,READ_ERR,arc,fileHd);
				return NULL;
			}
			} else {
				rc = fread (strInt,sizeof(char),SIZE_T_4,arc);
				if (rc != SIZE_T_4)
				{
					cl_exit (LISTING_FILE,READ_ERR,arc,fileHd);
					return NULL;
				}
			}
		fileHd[count - 1]->fileSize = strToInt (strInt,rc);
		if (!(fileHd[count - 1]->huff_ubits & NO_HUFF))
		{
			fseek (arc,2,SEEK_CUR);
			ch = rc;
			rc = fread (strInt,sizeof(char),ch,arc);
			if (rc != ch) 
			{
				cl_exit (LISTING_FILE,READ_ERR,arc,fileHd);
				return NULL;
			}
			fileHd[count - 1]->huff_file_len = strToInt (strInt,rc);
			fseek (arc,fileHd[count - 1]->huff_file_len + 4,SEEK_CUR);
		} else {
			fseek (arc,fileHd[count - 1]->fileSize,SEEK_CUR);
			fseek (arc,SIZE_T_4,SEEK_CUR);
		/*	fread (strInt,1,4,arc);
			printf ("%u\n",strToInt (strInt,4));*/
		}
		count ++;
	}
	fileHd = (fileHead **) realloc (fileHd,sizeof(fileHead *)*count);
	if (!fileHd)
		cl_exit (LISTING_FILE,MEMORY_ERR,arc,fileHd);
	fileHd[count - 1]=NULL;
	return fileHd;
}



